package com.onlinepay.demo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.onlinepay.entity.*;
import com.onlinepay.exception.EncryptException;
import com.onlinepay.service.impl.PaymentServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.ArrayList;
import java.util.List;


/**
 * @Author: linjl
 * @Date: 2025/3/12 12:00
 * @Description: demo
 */

@Slf4j
public class PaymentDemo {


    // 创建 Spring 容器
    private static final ApplicationContext context = new AnnotationConfigApplicationContext(PaymentServiceImpl.class);
    // 从容器中获取 MyService 的实例
    private static final PaymentServiceImpl paymentService = context.getBean(PaymentServiceImpl.class);

    //IpAddress
    private static final String IP_ADDRESS = "115.196.141.189";

    //RSA privateKey
    private static final String PRIVATE_KEY_RSA = "MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAhI37UQzrhjkwf1m09FI7AQCAxvbCDSoMvGw+PIDwS8kPjT+jQjnfEiPHXHsFO2RfxRXoyWLF87Aw2uNSkebtXQIDAQABAkA48rbSL3bQh6lqTSQNA3cOCyVIVaMw/KvUF7z1SX/zTqO4KLC65xq1St9iA315ctHCxif3MpTyascKVfErvYOBAiEAwmhunojYMzP9v/oL9BA/G4ulMjqxua4JrCzszbBSWRUCIQCujOZ3zHoGGFxSQoMltVgnVUNfZ8pJXNHknD4PnORFKQIgdS5blyWnI93v0trWf6uXTnQzA/7LB7/2xphhuG3ShS0CIQCDqBNN2RVQ857vDOwOzTGouU8P7yFNJI3MeWz2KcijwQIgJjoByI7vNpgDRsSrjjVg1Q+cGnZopkfvIhbIiuaWKZE=";

    //MD5 privateKey _bndqOD^
    private static final String PRIVATE_KEY_MD5 = "_bndqOD^";

    //URL
    private static final String PAYMENT_URL = "https://testpay.onlinepay.ai/payment";
//    private static final String PAYMENT_URL = "http://127.0.0.1:10010/payment";
    private static final String REFUND_URL = "https://testpay.onlinepay.ai/refund/apply";
//    private static final String REFUND_URL = "http://127.0.0.1:10010/refund/apply";

    private static final String REFERER_URL = "https://www.baidu.com";
    private static final String QUERY_URL = "https://testpay.onlinepay.ai/order/query";
    private static final String QUERY_LIST_URL = "https://testpay.onlinepay.ai/order/queryOrderList";

    public static void main(String[] args) {

        //encryption:RSA
//        cryptoPayment();

//        encryption:RSA
//        creditCardPayment();

        //encryption:MD5
//        query();

        //encryption:MD5
//        queryList();

        //encryption:RSA
//        refund();
    }

    //数字货币支付
    public static void cryptoPayment() {

        CryptoPaymentParams cryptoPaymentParams = new CryptoPaymentParams();
        //https://docs.onlinepay.ai/docs/currency-code
        cryptoPaymentParams.setMerNo("104001001");
        cryptoPaymentParams.setMerOrderNo(System.currentTimeMillis() + "");
        //RSA privateKey
        cryptoPaymentParams.setSign(PRIVATE_KEY_RSA);
        cryptoPaymentParams.setSourceAmount("1.00");
        cryptoPaymentParams.setCurrencyCode("USDT");
        cryptoPaymentParams.setIpAddress(IP_ADDRESS);
        cryptoPaymentParams.setReturnUrl("https://www.baidu.com");
        cryptoPaymentParams.setNotifyUrl("https://www.baidu.com");
        cryptoPaymentParams.setChain_id("TRON");
        cryptoPaymentParams.setVersion("V3.0.0");
        cryptoPaymentParams.setEmail("abcd@gmail.com");
        cryptoPaymentParams.setUserId("1152");
        cryptoPaymentParams.setBusinessType("1");
        cryptoPaymentParams.setPaymentMode("CRYPTO");
        cryptoPaymentParams.setPostUrl("https://testpay.onlinepay.ai/payment");

        String paymentType = "CRYPTO";

        try {
            JSONObject map = JSON.parseObject(JSON.toJSONString(cryptoPaymentParams));
            String result = paymentService.payment(map,PAYMENT_URL,REFERER_URL,paymentType);
            System.out.println(result);
        } catch (EncryptException | Exception e) {
            log.error("EncryptException", e);
        }
    }

    public static void creditCardPayment() {
        CreditCardPaymentParams creditCardPaymentParams = new CreditCardPaymentParams();

        creditCardPaymentParams.setMerNo(104001001);
        //RSA privateKey
        creditCardPaymentParams.setSign(PRIVATE_KEY_RSA);
        creditCardPaymentParams.setMerOrderNo(System.currentTimeMillis() + "");
        creditCardPaymentParams.setSourceAmount("1");
        creditCardPaymentParams.setCardNo("4242424242424242");
        creditCardPaymentParams.setCardExpireMonth("12");
        creditCardPaymentParams.setCardExpireYear("2027");
        creditCardPaymentParams.setCardSecurityCode("212");
        creditCardPaymentParams.setCurrencyCode("USD");
        creditCardPaymentParams.setIpAddress(IP_ADDRESS);
        creditCardPaymentParams.setReturnUrl("https://www.baidu.com");
        creditCardPaymentParams.setNotifyUrl("http://www.baidu.com");
        creditCardPaymentParams.setVersion("V3.0.0");
        creditCardPaymentParams.setBillingFirstName("xxx");
        creditCardPaymentParams.setBillingLastName("xx xxx");
        creditCardPaymentParams.setBillingAddress1("xx-xx xxxxx xxxx Astoria");
        creditCardPaymentParams.setBillingCity("New York");
        creditCardPaymentParams.setBillingState("NY");
        creditCardPaymentParams.setBillingZipCode("11105");
        creditCardPaymentParams.setBillingCountry("US");
        creditCardPaymentParams.setBillingEmail("xxxxx@qq.com");
        creditCardPaymentParams.setBillingPhone("12345678901");
        creditCardPaymentParams.setShippingFirstName("xx");
        creditCardPaymentParams.setShippingLastName("xx xxxx");
        creditCardPaymentParams.setShippingAddress1("xxxx xxx xx xx");
        creditCardPaymentParams.setShippingCity("New York");
        creditCardPaymentParams.setShippingState("NY");
        creditCardPaymentParams.setShippingCountry("US");
        creditCardPaymentParams.setShippingZipCode("11105");
        creditCardPaymentParams.setShippingEmail("xxxxx@qq.com");
        creditCardPaymentParams.setShippingPhone("12345678901");

        ProductInfo productInfo = new ProductInfo();
        productInfo.setSku("1");
        productInfo.setProductName("RB4165 Justin 622/55");
        productInfo.setPrice("1.00");
        productInfo.setQuantity("1");

        List<ProductInfo> list = new ArrayList<>();
        list.add(productInfo);
        creditCardPaymentParams.setProductInfoList(list);

        String paymentType = "ALLINPAY";

        try {
            JSONObject map = JSON.parseObject(JSON.toJSONString(creditCardPaymentParams));
            String result = paymentService.payment(map,PAYMENT_URL,REFERER_URL,paymentType);
            System.out.println(result);
        } catch (EncryptException | Exception e) {
            log.error("EncryptException", e);
        }
    }

    public static void query() {

        QueryParams queryParams = new QueryParams();
        queryParams.setMerNo("104001001");
        queryParams.setMerOrderNo("1741673750408");
        queryParams.setSign(PRIVATE_KEY_MD5);
        queryParams.setTradeNo("ONL02503111415586846");
        queryParams.setQueryType("sales");
        queryParams.setOperationType("query");
        queryParams.setIpAddress(IP_ADDRESS);
        queryParams.setReturnUrl("https://www.baidu.com");
        queryParams.setNotifyUrl("https://www.baidu.com");
        queryParams.setVersion("V3.0.0");

        try {
            String result = paymentService.query(queryParams,QUERY_URL,REFERER_URL);
            System.out.println(result);
        } catch (EncryptException | Exception e) {
            log.error("EncryptException", e);
        }

    }

    public static void queryList(){

        QueryListParams queryListParams = new QueryListParams();

        queryListParams.setMerNo("104001001");
        queryListParams.setMerOrderNo("1741673750408");
        queryListParams.setSign(PRIVATE_KEY_MD5);
        JSONObject json = new JSONObject();
        json.put("from", "2025-03-10 12:00:00");
        json.put("to", "2025-03-11 14:30:00");
        queryListParams.setDateLimit(json);
        queryListParams.setPageNum(1);
        queryListParams.setPageSize(15);
        queryListParams.setNotifyUrl("https://www.baidu.com");
        queryListParams.setVersion("V3.0.0");

        try {
            String result = paymentService.queryList(queryListParams,QUERY_LIST_URL,REFERER_URL);
            System.out.println(result);
        } catch (EncryptException | Exception e) {
            log.error("EncryptException", e);
        }
    }

    public static void refund(){

        RefundParams refundParams = new RefundParams();
        refundParams.setMerNo(104001001);
        refundParams.setMerOrderNo("1741673750408");
        refundParams.setTradeNo("ONL02503111415586846");
//        refundParams.setOperationType("refund");
        refundParams.setVersion("V3.0.0");
        refundParams.setAmount("1.00");
        refundParams.setRemark("test");
        refundParams.setNotifyUrl("http://www.baidu.com");
        refundParams.setSign(PRIVATE_KEY_RSA);

        try {
            JSONObject map = JSON.parseObject(JSON.toJSONString(refundParams));
            String result = paymentService.refund(map,REFUND_URL,REFERER_URL);
            System.out.println(result);
        } catch (EncryptException | Exception e) {
            log.error("EncryptException", e);
        }

    }
}
